/*
 * Copyright (c) 2025 Industrial Software Feature Database
 */
package com.comac.ins.web.config;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.stp.StpUtil;
import com.comac.ins.common.core.utils.ServletUtils;
import com.comac.ins.common.core.utils.SpringUtils;
import com.comac.ins.common.core.utils.StringUtils;
import com.comac.ins.common.satoken.utils.LoginHelper;
import com.comac.ins.common.security.config.SecurityConfig;
import com.comac.ins.common.security.config.properties.SecurityProperties;
import com.comac.ins.common.security.handler.AllUrlHandler;
import com.comac.ins.web.Interceptor.TokenInterceptor;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * sso拦截器
 *
 * @author hxloongs
 */
@Slf4j
@AutoConfiguration
@EnableConfigurationProperties(SecurityProperties.class)
@RequiredArgsConstructor
@AutoConfigureBefore(SecurityConfig.class)  // 确保先加载
public class TokenConfig implements WebMvcConfigurer {

    private final SecurityProperties securityProperties;

    private final TokenInterceptor tokenInterceptor; // 注入 SSO 拦截器

    /**
     * 注册sa-token的拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 1. 先注册 SSO 拦截器（order=0，优先级最高）
        registry.addInterceptor(tokenInterceptor)
            .order(0)
            .addPathPatterns("/**")
            .excludePathPatterns(securityProperties.getExcludes());
    }
}
